package nix.spring.proxy;

import java.lang.reflect.Method;

import nix.spring.service.UserService;
import nix.spring.service.UserServiceImpl;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

/**
 * cglib代理方式
 */
public class UserServiceProxyFactory2 implements MethodInterceptor {

	public UserService getUserServiceProxy() {

		Enhancer en = new Enhancer();
		en.setSuperclass(UserServiceImpl.class);
		en.setCallback(this);
		UserService us = (UserService) en.create();
		return us;
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] arg,
			MethodProxy methodProxy) throws Throwable {
		
		System.out.println("开启事务！");
		Object result = methodProxy.invokeSuper(obj, arg);
		System.out.println("关闭事务！");
		return result;
	}
}
